public class Solution19 {
/*
    public ListNode removeNthFromEnd(ListNode head, int n) {
        int len = 0;
        ListNode p = head;
        while (p != null) {
            len++;
            p = p.next;
        }
        int x = len - n;
        p = head;
        if (x == 0)
            return p.next;
        for (int i = 1; i < x; i++) {
            p = p.next;
        }
        p.next = p.next.next;
        return head;
    }
*/

    int cur = 0;

    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null) {
            return null;
        }
        head.next = removeNthFromEnd(head.next, n);
        cur++;
        if (cur == n)
            return head.next;
        else
            return head;
    }
}
